package com.zr.planClass.controller;

import com.zr.planClass.model.*;
import com.zr.planClass.service.PlanClassService;
import com.zr.util.*;
import jdk.nashorn.internal.runtime.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 非常狂躁的猪 on 2019/12/20.
 */
@RestController
@CrossOrigin //支持跨域
public class PlanClassController {

    @Autowired
    private PlanClassService planClassService;

    @PostMapping("planCategory/add") //添加
    public ResultVo addPlanClass(@RequestBody @Valid PlanClassAddVo planClassAddVo, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVo resultVo = planClassService.add(planClassAddVo);

        return resultVo;
    }

    @GetMapping("queryOnePlan")
    public ResultVo queryOnePlan(Integer id){
        if (id == null){
            return ResultBuilderVo.error(ErrorMessageClass.IDISNO);
        }
        ResultVo resultVo = planClassService.queryOnePlan(id);
        return resultVo;
    }


    @PostMapping("planCategory/update") //修改
    public ResultVo updatePlan(@RequestBody @Valid PlanClassUpdateVo planClassUpdateVo,BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }

        ResultVo resultVo = planClassService.update(planClassUpdateVo);

        return  resultVo;
    }

    @PostMapping("planCategory/queryPage") //分页
    public ResultVo queryAllPlan(@RequestBody PlanClassSelectVo planClassSelectVo){

        ResultVo resultVo = planClassService.queryAllPlan(planClassSelectVo);

        return  resultVo;
    }

    @PostMapping("planCategory/updateStatus") //修改状态
    public ResultVo updateStatus(@RequestBody @Valid PlanClassUpdateStatus planClassUpdateStatus,BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }

        ResultVo resultVo = planClassService.updateStatusById(planClassUpdateStatus);
        return  resultVo;
    }

    @PostMapping("metadata/getPullDownList") //下拉框procurement/getPullDownList
    public ResultVo getStatus(){

        Map<String,List> map = new HashMap<>();
        List<StatusVo> list2 = new ArrayList<>();
        for (TypeEnum t:TypeEnum.values() ){
            StatusVo statusVo = new StatusVo(t.getValue(),t.getName());
            list2.add(statusVo);
        }

        map.put("type",list2);

        List<StatusVo> list3 = new ArrayList<>();
        for (StatusEnum s:StatusEnum.values()){
            StatusVo statusVo = new StatusVo(s.getValue(),s.getName());
            list3.add(statusVo);
        }

        map.put("inquiryStatus",list3);

        List<StatusVo> list4 = new ArrayList<>();
        for (CurrencyCodeEnum c:CurrencyCodeEnum.values()){
            StatusVo statusVo = new StatusVo(c.getValue(),c.getName());
            list4.add(statusVo);
        }
        map.put("currencyCode",list4);

        List<StatusVo> list5 = new ArrayList<>();
        for (IsTaxEnum i:IsTaxEnum.values()){
            StatusVo statusVo = new StatusVo(i.getValue(),i.getName());
            list5.add(statusVo);
        }
        map.put("isTax",list5);




       /* List<StatusVo> list = new ArrayList<>();
        StatusVo statusVo = new StatusVo();
        statusVo.setValue(ErrorMessageEnum.JINYONG.getValue());
        statusVo.setLabel(ErrorMessageEnum.JINYONG.getName());
        list.add(statusVo);
        StatusVo statusVo2 = new StatusVo();
        statusVo2.setValue(ErrorMessageEnum.QIYONG.getValue());
        statusVo2.setLabel(ErrorMessageEnum.QIYONG.getName());
        list.add(statusVo2); */

        return ResultBuilderVo.success(map);
    }

    //quotation/InquiryIntroduction
   // @PostMapping("quotation/InquiryIntroduction")
    public ResultVo InquiryIntroduction(MultipartFile file) throws Exception {

        return planClassService.InquiryIntroduction(file);
    }

    @GetMapping("export")
    public ResultVo export(HttpServletResponse response,String code,String name,Integer status)throws Exception{
        return planClassService.export(response,code,name,status);
    }


}
























































